﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BLL;
using Model;
using Common;

namespace API.Controllers
{
    [RoutePrefix("api/ExperienceType")]
    public class ExperienceTypeController : ApiController
    {
        B_ExperienceType bet;
        public ExperienceTypeController()
        {
            bet = new B_ExperienceType();
        }

        /// <summary>
        /// 获取所有经历类别：分页
        /// </summary>
        /// <returns></returns>
        [Route("GetAll")]
        [HttpGet]
        public IHttpActionResult GetAll(int page,int size)
        {
            var data = from et in bet.GetAll()
                       select new ExperienceType
                       {
                           ETypeID = et.ETypeID,
                           EName = et.EName,
                           EPower = et.EPower,
                           IsShow = et.IsShow
                       };
            return Ok(new Result<List<ExperienceType>>() {
                Data = data.Skip((page - 1) * size).Take(size).ToList(),
                Msg = data.Count().ToString()
            });
        }

        /// <summary>
        /// 获取所有经历类别
        /// </summary>
        /// <returns></returns>
        [Route("GetAll")]
        [HttpGet]
        public IHttpActionResult GetAll()
        {
            var data = from et in bet.GetAll()
                       select new ExperienceType
                       {
                           ETypeID = et.ETypeID,
                           EName = et.EName,
                           EPower = et.EPower,
                           IsShow = et.IsShow
                       };
            return Ok(new Result<List<ExperienceType>>()
            {
                Data = data.ToList()
            });
        }

        /// <summary>
        /// 创建经历类别
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("Add")]
        [HttpPost]
        public IHttpActionResult Add(ExperienceType param)
        {
            try
            {
                bet.Add(param);
                return Ok(new Result<object>());
            }
            catch (Exception exp)
            {
                return Ok(new Result<object>() {
                    Code=500,
                    Msg=exp.Message
                });
            }
            
        }

        /// <summary>
        /// 删除经历分类
        /// </summary>
        /// <param name="id">分类ID</param>
        /// <returns></returns>
        [Route("Delete/{id}")]
        [HttpGet]
        public IHttpActionResult Delete(int id)
        {
            try
            {
                if (new B_ExperienceDetail().GetAll().Count(x=>x.ETypeID==id)>0)
                {
                    return Ok(new Result<object>()
                    {
                        Code = 500,
                        Msg = "无法删除"
                    });
                }
                bet.Delete(id);
                return Ok(new Result<object>());
            }
            catch (Exception exp)
            {
                return Ok(new Result<object>()
                {
                    Code = 500,
                    Msg = exp.Message
                });
            }

        }

        /// <summary>
        /// 查询经历分类详情
        /// </summary>
        /// <param name="id">分类ID</param>
        /// <returns></returns>
        [Route("Find/{id}")]
        [HttpGet]
        public IHttpActionResult Find(int id)
        {
            try
            {
                var data = bet.FindById(id);
                if (data == null)
                {
                    return Ok(new Result<object>
                    {
                        Code = 404,
                        Msg = "数据为空"
                    });
                }
                return Ok(new Result<Param_ExperienceType>
                {
                    Data = new Param_ExperienceType
                    {
                        ETypeID = data.ETypeID,
                        EName = data.EName,
                        EPower = data.EPower,
                        IsShow = data.IsShow
                    }
                });
            }
            catch (Exception exp)
            {
                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = exp.Message
                });
            }
        }
        


        /// <summary>
        /// 修改经历分类详情
        /// </summary>
        /// <param name="id"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("Update/{id}")]
        [HttpPost]
        public IHttpActionResult Update(int id,ExperienceType param)
        {
            try
            {
                var data = bet.FindById(id);
                if (data == null)
                {
                    return Ok(new Result<object>
                    {
                        Code = 404,
                        Msg = "数据为空"
                    });
                }
                if (bet.GetAll().Count(x=>x.EName==param.EName)==0 || data.EName==param.EName)
                {
                    bet.Update(id, param);
                    return Ok(new Result<object>());
                }
                else
                {
                    return Ok(new Result<object>
                    {
                        Code = 500,
                        Msg = "分类已存在"
                    });
                }
            }
            catch (Exception exp)
            {
                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = exp.Message
                });
            }
        }
    }
}
